#include "DynamicLengthSerializer.h"
#include "../Base/Base.h"

CDynamicLengthSerializer::CDynamicLengthSerializer(std::vector<char> & rVec, bool bWriteMode)
	: m_bWriteMode(bWriteMode)
	, m_dynaPool(rVec)
{
}

CDynamicLengthSerializer::~CDynamicLengthSerializer(void)
{
}

bool CDynamicLengthSerializer::IsWriteMode()
{
	return m_bWriteMode;
}

void CDynamicLengthSerializer::Serialize(bool & rBoolData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.AddBit(rBoolData);
	}
	else
	{
		rBoolData = m_dynaPool.GetBit();
	}
}

void CDynamicLengthSerializer::Serialize(char & rCharData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rCharData, sizeof(char));
	}
	else
	{
		m_dynaPool.Read((char *)&rCharData, sizeof(char));
	}
}

void CDynamicLengthSerializer::Serialize(unsigned char & rUCharData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rUCharData, sizeof(unsigned char));
	}
	else
	{
		m_dynaPool.Read((char *)&rUCharData, sizeof(unsigned char));
	}
}

void CDynamicLengthSerializer::Serialize(short & rShortData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rShortData, sizeof(short));
	}
	else
	{
		m_dynaPool.Read((char *)&rShortData, sizeof(short));
	}
}

void CDynamicLengthSerializer::Serialize(unsigned short & rUShortData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rUShortData, sizeof(unsigned short));
	}
	else
	{
		m_dynaPool.Read((char *)&rUShortData, sizeof(unsigned short));
	}
}

void CDynamicLengthSerializer::Serialize(int & rIntData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rIntData, sizeof(int));
	}
	else
	{
		m_dynaPool.Read((char *)&rIntData, sizeof(int));
	}
}

void CDynamicLengthSerializer::Serialize(unsigned int & rUIntData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rUIntData, sizeof(unsigned int));
	}
	else
	{
		m_dynaPool.Read((char *)&rUIntData, sizeof(unsigned int));
	}
}

void CDynamicLengthSerializer::Serialize(__int64 & rInt64Data)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rInt64Data, sizeof(__int64));
	}
	else
	{
		m_dynaPool.Read((char *)&rInt64Data, sizeof(__int64));
	}
}

void CDynamicLengthSerializer::Serialize(unsigned __int64 & rUInt64Data)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rUInt64Data, sizeof(unsigned __int64));
	}
	else
	{
		m_dynaPool.Read((char *)&rUInt64Data, sizeof(unsigned __int64));
	}
}

void CDynamicLengthSerializer::Serialize(float & rFloatData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rFloatData, sizeof(float));
	}
	else
	{
		m_dynaPool.Read((char *)&rFloatData, sizeof(float));
	}
}

void CDynamicLengthSerializer::Serialize(double & rDoubleData)
{
	if (m_bWriteMode)
	{
		m_dynaPool.Write((const char *)&rDoubleData, sizeof(double));
	}
	else
	{
		m_dynaPool.Read((char *)&rDoubleData, sizeof(double));
	}
}

void CDynamicLengthSerializer::Serialize(std::string & rStrData)
{
	if (m_bWriteMode)
	{
		unsigned int nSize = (unsigned int)rStrData.size();
		m_dynaPool.Write((const char *)&nSize, sizeof(nSize));
		m_dynaPool.Write(rStrData.c_str(), nSize);
	}
	else
	{
		unsigned int nSize;
		m_dynaPool.Read((char *)&nSize, sizeof(nSize));
		rStrData.assign(nSize,' ');
		m_dynaPool.Read((char*)rStrData.c_str(),nSize);
	}
}

void CDynamicLengthSerializer::Serialize(char* pData,int nSize)
{
	if (nSize <= 0)
	{
		ASSERT(nSize > 0);
		return;
	}
	if (m_bWriteMode)
	{
		m_dynaPool.Write(pData, (size_t)nSize);
	}
	else
	{
		m_dynaPool.Read(pData, (size_t)nSize);
	}
}
